package com.lishe.lishe.lishe.module.excitation.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lishe.lishe.R;
import com.lishe.lishe.lishe.base.BaseActivity;
import com.lishe.lishe.lishe.common.Globals;
import com.lishe.lishe.lishe.module.excitation.bean.user.UserObject;
import com.lishe.lishe.lishe.module.excitation.presenter.ExcitationPresenter;
import com.lishe.lishe.lishe.utils.LoadingUtils;
import com.lishe.lishe.retrofit.DataResponseListener;

/**
 * Created by yang on 2017/11/24.
 */

public class PersonModifyGenderActivity extends BaseActivity implements View.OnClickListener {

    public static final void jump(Activity activity, UserObject user) {
        Intent intent = new Intent(activity, PersonModifyGenderActivity.class);
        mUserObject = user;
        activity.startActivityForResult(intent, PersonMessageModifyActivity.REQUEST_CODE_MODIFY_GENDER);
    }

    private LinearLayout mLinearMan,
            mLinearWomen;
    private ImageView mImageMan,
            mImageWomen;
    private Button mButtonBack;
    private TextView mTextTitle;

    private static UserObject mUserObject;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_modify_gender);

        initView();
        setListener();
    }

    @Override
    public void onBackPressed() {
        back();
    }

    private void initView() {
        mLinearMan=findViewById(R.id.ll_man);
        mLinearWomen=findViewById(R.id.ll_women);
        mImageMan=findViewById(R.id.man);
        mImageWomen=findViewById(R.id.women);
        mButtonBack=findViewById(R.id.title_left_button);
        mTextTitle=findViewById(R.id.title_text_title);

        mTextTitle.setText(R.string.gender);
        if(!TextUtils.isEmpty(mUserObject.getGender())) {
            if(mUserObject.getGender().equals("M")){
                mImageMan.setVisibility(View.VISIBLE);
            }
            if(mUserObject.getGender().equals("F")){
                mImageWomen.setVisibility(View.VISIBLE);
            }
        }
    }

    private void setListener() {
        mLinearMan.setOnClickListener(this);
        mLinearWomen.setOnClickListener(this);
        mButtonBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_left_button:
                back();
                break;
            case R.id.ll_man:
                mImageMan.setVisibility(View.VISIBLE);
                mImageWomen.setVisibility(View.INVISIBLE);
                ExcitationPresenter.getInstance().updatePersonGender(Globals.getToken(),"M",listener);
                break;
            case R.id.ll_women:
                mImageMan.setVisibility(View.INVISIBLE);
                mImageWomen.setVisibility(View.VISIBLE);
                ExcitationPresenter.getInstance().updatePersonGender(Globals.getToken(),"F",listener);
                break;
        }
    }

    private void back(){
        setResult(RESULT_OK,null);
        finish();
    }

    private DataResponseListener<String> listener=new DataResponseListener<String>() {

        @Override
        public void onPrepare() {
            LoadingUtils.createLoadingDialog(PersonModifyGenderActivity.this,"请稍候...",false);
        }

        @Override
        public void onSuccess(String gender) {
            mUserObject.setGender(gender);
            LoadingUtils.closeDialog();
            back();
        }

        @Override
        public void onFail(String failMessage) {
            toast(failMessage);
            LoadingUtils.closeDialog();
        }

        @Override
        public void onError(Throwable e) {
            e.printStackTrace();
            LoadingUtils.closeDialog();
        }
    };
}
